#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<windows.h>
#include<stdlib.h>



struct stu {
    char name[10];
    int age;
};
int cmp_int(const void* e1, const void* e2) {
    return *(int*)e1 - *(int*)e2;
}
int cmp_s_age(const void* e1, const void* e2) {
    return ((struct stu*)e1)->age - ((struct stu*)e2)->age;
}
int cmp_s_name(const void* e1, const void* e2) {
    return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
}


void Swap(char* r1, char* r2,int sz) {
    int i = 0;
    for (i = 0; i < sz; i++) {
        char temp = *r1;
        *r1 = *r2;
        *r2 = temp;
        r1++;
        r2++;
    }
}
void my_sort(void* base, int sz, int y, int (*cmp)(const void* e1, const void* e2)) {
    int i = 0;
    for (i = 0; i < sz-1; i++) {
        int j = 0;
        for (j = 0; j < sz - 1 - i; j++) {
            if (cmp((char*)base + j * y, (char*)base + (j + 1) * y) > 0) {
                Swap((char*)base + j * y, (char*)base + (j + 1) * y, y);
            }
                
        }
    }
}

int main() {
    struct stu s[3] = { {"zs",20},{"ls",40},{"ww",30} };
    int arr[5] = { 5,4,3,2,1 };
    //qsort(arr, 5, 4, cmp_int);
    my_sort(arr, 5, 4, cmp_int);
    int i = 0;
    for (i = 0; i < 5; i++)printf("%d ", arr[i]);
    //my_sort(s, 3, sizeof(s[0]), cmp_s_age);
    my_sort(s, 3,  sizeof(s[0]), cmp_s_name);
    printf("\n");
    for (i = 0; i < 3; i++) {
        printf("%s  %d    \n", s[i].name, s[i].age);
    }
    return 0;
}